import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.swing.SwingUtilities;


public class Controller 
{
	private Model model;
	private View view;
	ThreadPoolExecutor executor;
	
	
	public Controller(Model m, View v)
	{
		this.model = m;
		this.view = v;
		
		this.view.addFacultyListener(new FacultyListener());
		this.view.addQueryListener(new QueryListener());
		this.view.addSelectionListeners(new SelectionListener());
		this.view.addQuitListener(new QuitListener());
		this.view.addConsoleListener(new ConsoleListener());
		
		model.addObserver(view);
		model.notifyObservers();
		ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(5);

		executor =new ThreadPoolExecutor(2,10,100,TimeUnit.MILLISECONDS, queue);
	}
	
	class FacultyListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) 
		{	
			System.out.println("adding faculty");
			model.addFaculty(view.getCollege(), view.getFaculty(),view.getAddress(), view.getField());
			model.notifyObservers();
			view.cf.setVisible(false);
		}
		
	}
	
	class QueryListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{	
			if(view.getMainFrame().checkInputs())
			{
				view.displayProgress();
				String firstFaculty =  view.getMainFrame().getFirst().getSelectedFaculty();
				String firstCollege =  view.getMainFrame().getFirst().getSelectedCollege();
				String secondFaculty = view.getMainFrame().getSecond().getSelectedFaculty();
				String secondCollege = view.getMainFrame().getSecond().getSelectedCollege();
				
				Faculty one = model.getFaculty(firstCollege, firstFaculty);
				Faculty two = model.getFaculty(secondCollege, secondFaculty);
				
				model.setSelected(one, two);
				
				DoItAllWorker diaw = new DoItAllWorker(model.selected1, firstCollege, model.selected2, secondCollege, view);
				diaw.execute();
			}
		}
	}
	
	class ResetListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			model.reset();
			view.reset();
		}
		
	}
	
	class ConsoleListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{	
			view.getConsole().setVisible(true);
		}
	}
	
	class SelectionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{	
			view.getMainFrame().updateFacultyDropdowns();
		}
	}
	
	class  QuitListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			model.saveColleges();
			view.getMainFrame().dispose();
			System.exit(0);	
		}
		
	}
	
	class TextListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			view.console.append(e.getActionCommand());
		}
		
	}
}
